package br.fatec.webservice.client;

import br.fatec.webservice.flamelws.FlamelWS;
import br.fatec.webservice.flamelws.FlamelWSService;
import br.fatec.webservice.model.Cliente;
import br.fatec.webservice.model.Formula;

public class ClientFlamelWs {

	private FlamelWS flamelWs;

	public ClientFlamelWs() {
		FlamelWSService service = new FlamelWSService();
		flamelWs = service.getFlamelWS();
	}

	public String login(String usuario, String senha, String droidId) {
		return flamelWs.login(usuario, senha, droidId);
	}

	public String cadastrarCliente(br.fatec.model.entity.Cliente cliente) throws Exception {

		Cliente clienteWs = converterCliente(cliente);
		
		return flamelWs.cadastrarCliente(clienteWs.getXml());
	}

	public String excluirCliente(br.fatec.model.entity.Cliente cliente) throws Exception {
		
		Cliente clienteWs = converterCliente(cliente);
		
		return flamelWs.excluirCliente(clienteWs.getXml());
	}

	public String cadastrarFormula(br.fatec.model.entity.Formula formula) throws Exception {
		
		Formula formulaWs = converterFormula(formula);
		
		return flamelWs.cadastrarFormula(formulaWs.getXml());
	}
	
	public String excluirFormula(br.fatec.model.entity.Formula formula) throws Exception {
		
		Formula formulaWs = converterFormula(formula);
		
		return flamelWs.excluirFormula(formulaWs.getXml());
	}
	
	private Cliente converterCliente(br.fatec.model.entity.Cliente cliente)
	{
		br.fatec.webservice.model.Cliente clienteWs = new br.fatec.webservice.model.Cliente();
		
		clienteWs.setCodigo(cliente.getCodigo());
		clienteWs.setNome(cliente.getNome());
		clienteWs.setUsuario(cliente.getLogin().getNomeUsuario());
		clienteWs.setSenha(cliente.getLogin().getSenha());
		
		return clienteWs;
	}
	
	private Formula converterFormula(br.fatec.model.entity.Formula formula)
	{
		br.fatec.webservice.model.Cliente clienteWs = new br.fatec.webservice.model.Cliente();
		
		clienteWs.setCodigo(formula.getVenda().getCliente().getCodigo());
		clienteWs.setNome(formula.getVenda().getCliente().getNome());
		clienteWs.setUsuario(formula.getVenda().getCliente().getLogin().getNomeUsuario());
		clienteWs.setSenha(formula.getVenda().getCliente().getLogin().getSenha());
		
		br.fatec.webservice.model.Formula formulaWs = new br.fatec.webservice.model.Formula();
		
		formulaWs.setCodigo(formula.getCodigo());
		formulaWs.setNome(formula.getNome());
		formulaWs.setData(formula.getCriadoEm());
		formulaWs.setStatus(formula.getStatus());
		formulaWs.setCliente(clienteWs);
		
		return formulaWs;
	}
}
